package com.ruoyi.web.controller.tool;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.embedding.EmbeddingUtils;
import com.ruoyi.framework.datasource.RuntimeDataSourceSetting;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/test/user")
@AllArgsConstructor
public class TestController extends BaseController {

    private RuntimeDataSourceSetting runtimeDataSourceSetting;
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();

    {
        users.put(1, new UserEntity(1, "mapper/admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }



    @GetMapping("test")
    public List<Float> createMultimodalEmbeddings(String segmentContent,String type) {
        return EmbeddingUtils.createMultimodalEmbeddings(segmentContent,type);
        // 输出结果
    }


    @RequestMapping("/testSaToken")
    public void testSaToken() {
// 获取：当前账号所拥有的权限集合
        StpUtil.login("1");
        String tokenName = StpUtil.getTokenName();
        StpUtil.getPermissionList();
    }

    @RequestMapping("/testSaToken1")
    public void testSaToken1() {
// 获取：当前账号所拥有的权限集合
        List<String> permissionList = StpUtil.getPermissionList();
        logger.info(JSONObject.toJSONString(permissionList));
    }

    @Operation(tags = "获取用户列表")
    @GetMapping("/list")
    public R<List<UserEntity>> userList() {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return R.ok(userList);
    }

    @Operation(tags = "获取用户详细")
    @GetMapping("/{userId}")
    public R<UserEntity> getUser(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            return R.ok(users.get(userId));
        } else {
            return R.fail("用户不存在");
        }
    }

    @Operation(tags = "新增用户")
    @PostMapping("/save")
    public R<String> save(UserEntity user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @Operation(tags = "更新用户")
    @PutMapping("/update")
    public R<String> update(@RequestBody UserEntity user) {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
            return R.fail("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId())) {
            return R.fail("用户不存在");
        }
        users.remove(user.getUserId());
        users.put(user.getUserId(), user);
        return R.ok();
    }

    @Operation(tags = "删除用户信息")
    @DeleteMapping("/{userId}")
    public R<String> delete(@PathVariable Integer userId) {
        if (!users.isEmpty() && users.containsKey(userId)) {
            users.remove(userId);
            return R.ok();
        } else {
            return R.fail("用户不存在");
        }
    }


    public static void main(String[] args) {
      /*  long l = DateUtil.betweenMonth(DateUtils.parseDate("2025-01"), DateUtils.parseDate("2026-01"), false);
        System.out.println(l);
        int a = (int) l / 5;
        System.out.println(a + 1);

        System.out.println(DateUtil.offsetYear(DateUtils.parseDate("2026-01"),-1));
        Date date = DateUtils.parseDate("2032-06");
        DateTime dateTime2 = DateUtil.offsetMonth(date, -1);
        DateTime dateTime = DateUtil.offsetYear(dateTime2, -60);
        System.out.println(dateTime.toString());
        DateTime dateTime1 = DateUtil.offsetMonth(dateTime, -(a));
        System.out.println(dateTime1.toString());
*/

        long l = DateUtil.betweenMonth(DateUtils.parseDate("2025-01"), DateUtils.parseDate("2026-01"), true);
        System.out.println(l);
    }
}

@Data
@Schema(description = "数据源实体")
class DataSourceEntity {
    private String url;
    private String userName;
    private String passWord;
    private String driverClassName;
    private String dataSourceName;
}

@Schema(description = "用户实体")
class UserEntity {
    @Schema(description = "用户ID")
    private Integer userId;

    @Schema(description = "用户名称")
    private String username;

    @Schema(description = "用户密码")
    private String password;

    @Schema(description = "用户手机")
    private String mobile;

    public UserEntity() {

    }

    public UserEntity(Integer userId, String username, String password, String mobile) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
